<?php
declare (strict_types = 1);

namespace app\api\controller;

use thans\jwt\facade\JWTAuth;
use think\exception\ValidateException;
use think\Request;

class Login
{
    /**
     * 接收参数、实现登录
     * @param Request $request
     */
    public function doLogin(Request $request){

        $username = $request->post('username');
        $password = $request->post('password');
        # 必填、长度验证
        try {

            validate(['username' => 'require|length:3,8','password' => 'require|min:6'])->check([
                'username' => $username,
                'password' => $password,
            ]);

            # 连接数据库 、判断用户名密码。。。
            # 模拟
            $admin_id = 1;
            $token = JWTAuth::builder(['admin_id' => $admin_id]);

            return responsed(200,'ok',['token' => $token]);

        }catch (ValidateException $validateException) {
            # 响应方法、可以自己封装
           return responsed($validateException->getCode(),$validateException->getMessage());
        }
    }
}
